package cn.letcode.sample;

import java.util.Arrays;

/**
 * Created by 31464 on 2021/10/20.
 * 453. 最小操作次数使数组元素相等
 * https://leetcode-cn.com/problems/minimum-moves-to-equal-array-elements/
 */
public class MinimumMovesToEqualArrayElements {

    public static void main(String[] args) {
        int nums[]={1,2,3,4,5};

        useMinMoves(nums);
        useMaxMoves(nums);

    }

    /**
     * 当前原始值最小值后求和
     * @param nums
     */
    private static void useMinMoves(int[] nums) {
        int minNums= Arrays.stream(nums).min().getAsInt();
        int res=0;
        for(int num:nums){
            res+=num-minNums;
        }
        System.out.println(res);
    }


    /**
     * 最大值减去元素值求和
     * @param nums
     */
    private static void useMaxMoves(int[] nums) {
        int maxNums= Arrays.stream(nums).max().getAsInt();
        int res=0;
        for(int num:nums){
            res+=maxNums-num;
        }
        System.out.println(res);
    }


}
